<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" @click="add">+新增角色</el-button>
          <el-table :data="list">
            <el-table-column label="序号" type="index">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button
                    type="text"
                    @click="setpower(row.id)"
                  >分配权限</el-button>
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination">
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 10, 20, 100, 200, 300, 400]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @current-change="currentChange"
              @size-change="sizeChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <component :is="'info'" lazy />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 弹框组件 -->
    <DiaLogItem
      ref="dialogitem"
      v-model="show"
      :list="list"
      @currentChange="currentChange"
      @getData="getData"
    />
    <!-- 分配权限弹框组件 -->
    <DiaLogPower ref="dialogpower" v-model="showPower" />
  </div>
</template>

<script>
import DiaLogItem from '@/views/setting/components/dialogItem.vue'
import DiaLogPower from './components/DialogPower.vue'
import { sysRole, sysRoleDel } from '@/api/setting'
export default {
  components: {
    DiaLogItem,
    DiaLogPower,
    info: () => import('@/views/setting/components/info.vue')
  },
  data() {
    return {
      show: false,
      showPower: false,
      page: {
        page: 1,
        pagesize: 10,
        total: 100
      },
      list: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    //  获取所有角色列表
    async getData() {
      const res = await sysRole(this.page)
      this.list = res.data.rows
      this.page.total = res.data.total
      console.log('角色列表', res)
    },
    //  页码改变时
    currentChange(page) {
      this.page.page = page
      this.getData()
    },
    //  页容量改变时
    sizeChange(size) {
      this.page.pagesize = size
      this.page.page = 1
      this.getData()
    },
    //  添加角色
    add() {
      this.show = true
      this.$refs.dialogitem.mode = 'add'
    },
    // 编辑juese
    edit(row) {
      this.show = true
      this.$refs.dialogitem.mode = 'edit'
      this.$refs.dialogitem.form = JSON.parse(JSON.stringify(row))
    },
    //  删除
    del(row) {
      this.$confirm('确定要删除吗?', '提示')
        .then(async() => {
          await sysRoleDel(row.id)
          this.$message.success('删除成功')
          this.currentChange(1)
        })
        .catch(() => {})
    },
    //  权限分配
    setpower(id) {
      this.showPower = true
      this.$refs.dialogpower.getUserInfo(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 30px;
  .pagination {
    //
    text-align: right;
  }
}
</style>
